/**
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2010  Sean Beecroft

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * @version 1.10 2010-01-01
 * @author Sean Beecroft
 */
package yawiki.server.application;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import yawiki.server.database.*;
import yawiki.server.util.StringUtility;

import java.sql.*;
import java.net.*;

public class Pump extends HttpServlet {

    DbConnectionBroker broker;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        // The below statement sets up a Broker with a minimun pool size of 2 connections
        // and a maximum of 5.  The log file will be created in 
        // yawiki.log and the pool connections will be
        // restarted once a day.

        try {
            broker = new DbConnectionBroker("org.postgresql.Driver",
                    "jdbc:postgresql:yawiki", "postgres", "XXXXXX", 5, 24,
                    "yawiki.log", 1.0);
        } catch (IOException e5) {
            System.out.println(e5);
            e5.printStackTrace();
        }
    }

    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        try {
            String page = request.getParameter("page"); // "this is a big test";
            Connection conn = null;
            request.setCharacterEncoding("UTF-8");

            try {
                page = page.replaceAll("_", " ");
                page = StringUtility.capitalize(page);
                page = URLDecoder.decode(page, "UTF-8");

                System.out.println("Pump Page:" + page);

            } catch (Exception ex) {
                System.out.println(ex);
            }

            System.out.println("Pump called");
            response.setContentType("text/html; charset=utf-8");
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"));


            String pageTitle = page.replaceAll("'", "&apos;");
            String sql = "SELECT * from yawiki_resource where title = '" + pageTitle + "'";

            conn = broker.getConnection();
            int con = broker.idOfConnection(conn);
            System.out.println("using:" + con);
            Statement statement = null;
            statement = conn.createStatement();

            ResultSet r = statement.executeQuery(sql);
            if (r.next()) {

                String data = r.getString("data");
                data = data.replaceAll("&amp;", "&");
                data = data.replaceAll("&apos;", "'");
                data = data.replaceAll("&gt;", ">");
                data = data.replaceAll("&lt;", "<");
                data = data.replaceAll("&quot;", "\"");


                out.write(data);
                out.flush();
            } else {
                out.write("==New Page==");
            }

            r.close();
            statement.close();
            statement = null;


            // The connection is returned to the Broker
            broker.freeConnection(conn);

            out.flush();
            out.close();

            System.out.println("Pump finished.");

        } catch (Exception ex) {
            System.out.println(ex);
        }

    }

    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {
        doGet(request, response);
    }
}
